<template>
  <common_print_dialog ref="common_print_dialog" singlePrintNumber :templateVisiable="!thirdParty"
    businessType="OUTBOUND_WAYBILL" v-bind="$attrs" v-on="$listeners" :tableModel="tableModel" :printUrl="printUrl"
    :printHandler="printHandler" :previewUrl="previewUrl" :previewHandler="previewHandler" :urlGetFun="urlGetFun" />
</template>

<script>
import common_print_dialog from './common_print_dialog.vue'

export default {
  components: { common_print_dialog },
  props: {
    tableModel: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      previewUrl: "/wms-api/outbound/OutboundOverseasOrderRpc/previewWaybillByTrackNumber.json",
      printUrl: "",
      thirdParty: false,
    }
  },
  methods: {
    printDefaultOption(params) {
      this.$refs.common_print_dialog.printDefaultOption(params)
    },
    urlGetFun(row) {
      if (this.thirdParty) {
        return { url: row.labelOssPath, oss: true }
      } else {
        return null
      }
    },
    previewHandler(data, row) {
      data.trackNumber = row.trackNumber;
      data.platformCode = row.platformCode
      data.printerName = 'none'
      data.clientName = 'none'
      return data
    },
    printHandler(data, list) {
      if (this.thirdParty) {
        return {
          paramList: list.map(it => {
            return {
              clientId: data.clientName,
              printerName: data.printerName,
              printCount: data.printNumSum,
              trackNumber: it.trackNumber,
              fileName: it.trackNumber,
              fileType: this.$reg.FileExt(it.labelOssPath),//data.fileType,
              labelOssPath: it.labelOssPath,
            }
          })
        }
      } else {
        data.printTrackNumberList = list.map(it => (
          {
            trackNumber: it.trackNumber,
            platformCode: it.platformCode,
            printNum: it.printNum,
          }
        ))
      }

      return data
    },
    show(rows, option) {
      let thirdParty = !!(option && option.thirdParty)
      this.thirdParty = !!thirdParty
      if (!!thirdParty) {
        this.printUrl = "/wms-api/outbound/OutboundBoxPackageRpc/printPdfList.json"
      } else {
        this.printUrl = "/wms-api/outbound/OutboundOverseasOrderRpc/printWaybillByTrackNumber.json"
      }
      this.$refs.common_print_dialog.show(rows, Object.assign({
        title: thirdParty ? this.$tt('label_print_any', { type: this.$t('label_third_party_face_track') }) : null,
        url: this.printUrl,
      }, Object.assign({ templateRequire: !thirdParty }, option)))
      // this.$nextTick(() => {
      //   this.$refs.common_print_dialog.show(rows, Object.assign({
      //     title: thirdParty ? this.$tt('label_print_any', { type: this.$t('label_third_party_face_track') }) : null,
      //     url: this.printUrl,
      //   }, Object.assign({ templateRequire: !thirdParty }, option)))
      // })
    },
  }
}
</script>
